package com.sskj.gocoin.utils;

import android.content.Context;
import android.support.design.widget.BottomSheetDialog;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.sskj.gocoin.MyAppLication;
import com.sskj.gocoin.R;
import com.sskj.gocoin.common.App;
import com.sskj.gocoin.m.HttpConfig;
import com.sskj.gocoin.m.bean.HttpData;
import com.sskj.gocoin.m.http.JsonCallBack;
import com.sskj.gocoin.p.base.BasePresenter;

public class BottomUpUtil {
    public static BottomSheetDialog getPay(Context context, BasePresenter basePresenter, OnPwdGet onPwdGet) {

        BottomSheetDialog codeDialog = new BottomSheetDialog(context);
        View dialogView = LayoutInflater.from(context)
                .inflate(R.layout.bottom_sheet_fabi_check_code, null);
        EditText mPayPwdEditText = dialogView.findViewById(R.id.et_code);
        ClickUtil.click(dialogView.findViewById(R.id.tv_cancel), codeDialog::dismiss);
        Button mSubmit = dialogView.findViewById(R.id.bt_submit);
        codeDialog.setContentView(dialogView);

        mPayPwdEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() >= 8) {
                    mSubmit.setEnabled(true);
                } else {
                    mSubmit.setEnabled(false);
                }
            }
        });

        ClickUtil.click(mSubmit, () -> { //提交修改

            String pwd = mPayPwdEditText.getText().toString().trim();
            if (TextUtils.isEmpty(pwd)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strBottomUpUtil200));
                return;
            }
            if (!CheckUtil.isPayPwd(pwd)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strBottomUpUtil201));
                return;
            }
            onPwdGet.onSure(pwd);

        });
        return codeDialog;

    }

    public static BottomSheetDialog makePay(Context context, BasePresenter basePresenter, OnPwdSure onPwdSure) {

        BottomSheetDialog codeDialog = new BottomSheetDialog(context);
        View dialogView = LayoutInflater.from(context)
                .inflate(R.layout.bottom_sheet_fabi_check_code, null);
        EditText mPayPwdEditText = dialogView.findViewById(R.id.et_code);
        ClickUtil.click(dialogView.findViewById(R.id.tv_cancel), () -> {
            codeDialog.dismiss();
        });
        Button mSubmit = dialogView.findViewById(R.id.bt_submit);
        codeDialog.setContentView(dialogView);
        mPayPwdEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() >= 8) {
                    mSubmit.setEnabled(true);
                } else {
                    mSubmit.setEnabled(false);
                }
            }
        });

        ClickUtil.click(mSubmit, () -> { //提交修改

            String pwd = mPayPwdEditText.getText().toString().trim();
            if (TextUtils.isEmpty(pwd)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strBottomUpUtil200));
                return;
            }
            if (!CheckUtil.isPayPwd(pwd)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strBottomUpUtil201));
                return;
            }

            OkGo.<HttpData>post(HttpConfig.BASE_URL + HttpConfig.CHECK_PAY_PASS)
                    .params("mobile", MyAppLication.getMobile())
                    .params("tpwd", pwd)
                    .execute(new JsonCallBack<HttpData>(basePresenter) {
                        @Override
                        public void _onSuccess(Response<HttpData> response) {
                            HttpData httpListData = response.body();
                            if (httpListData.getStatus() == 200) {
                                onPwdSure.onSure();
                            } else {
                                ToastUtil.showShort(httpListData.getMsg());
                            }
                        }
                    });
        });
        return codeDialog;

    }

    public interface OnPwdSure {
        void onSure();
    }

    public interface OnPwdGet {
        void onSure(String pwd);
    }
}
